package com.vp.org.web.servlet;

import com.vp.org.dao.BookDao;
import com.vp.org.dao.RemarkDao;
import com.vp.org.dao.impl.BookDaoImpl;
import com.vp.org.dao.impl.RemarkDaoImpl;
import com.vp.org.pojo.AdminInfo;
import com.vp.org.pojo.BookInfo;
import com.vp.org.pojo.Remark;
import com.vp.org.pojo.UserInfo;
import com.vp.org.web.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({"FieldCanBeLocal", "MismatchedQueryAndUpdateOfCollection"})
@WebServlet("/remark")
public class RemarkServlet extends BaseServlet {

    private UserInfo userInfo = null;
    private Remark remark = null;
    private RemarkDao remarkDao = new RemarkDaoImpl();
    private AdminInfo adminInfo = null;
    private BookInfo bookInfo = null;
    private BookDao bookDao = new BookDaoImpl();


    public void addRemark(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        remark = new Remark();
        userInfo = new UserInfo();
        bookInfo = new BookInfo();

        String userId = request.getParameter("userId");
        String bookId = request.getParameter("bookId");
        String content = request.getParameter("content");
        String rank = request.getParameter("rank");

        userInfo.setUserId(Integer.parseInt(userId));
        bookInfo.setId(Integer.parseInt(bookId));
        remark.setContent(content);
        remark.setRank(Integer.parseInt(rank));


        if (remarkDao.addRemark(userInfo,bookInfo,remark) > 0) {
            // 添加成功
            request.setAttribute("addSuccess",1);
            request.getRequestDispatcher("/admin/remarkAdd.jsp").forward(request,response);
//            response.sendRedirect("/admin/remarkAdd.jsp");
        } else {
            // 添加失败
            response.sendRedirect("/admin/error.jsp");
        }

    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        userInfo = new UserInfo();
        bookInfo = new BookInfo();
        remark = new Remark();

        String content = request.getParameter("content");
        String rank = request.getParameter("rank");
        String userId = request.getParameter("userId");
        String bookId = request.getParameter("bookId");

        userInfo.setUserId(Integer.parseInt(userId));
        bookInfo.setId(Integer.parseInt(bookId));
        remark.setContent(content);
        remark.setRank(Integer.parseInt(rank));


        if (remarkDao.addRemark(userInfo,bookInfo,remark) > 0) {
            response.sendRedirect("/user/center/user_1.jsp?pl=1");
        } else {
            response.sendRedirect("/user/center/user_1.jsp?pl=0");
        }

    }

    public void deleteRemark(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        remark = new Remark();
        userInfo = new UserInfo();

        String id = request.getParameter("id");
        String user_id = request.getParameter("user_id");

        remark.setId(Integer.parseInt(id));
        userInfo.setUserId(Integer.parseInt(user_id));

        if (remarkDao.deleteRemark(userInfo,remark) > 0) {
            // 删除成功
            // 重定向
            response.sendRedirect("http://localhost:8080/remark?method=findByPage&pageIndex2=1");
        } else {
            // 删除失败
            response.sendRedirect("/admin/error.jsp");
        }

    }

    public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收用户点击的上一页，下一页或者是指定的页数
        int pageIndex2 = Integer.parseInt(request.getParameter("pageIndex2"));

        adminInfo = new AdminInfo();
        adminInfo.setAdmin("admin");

        // 评论分页查询,默认第一页
        request.getSession().setAttribute("remarks", remarkDao.queryPageRemark(pageIndex2));
        // 评论分页总页数
        request.getSession().setAttribute("remarkPage", remarkDao.remarkPages());
        // 评论存储是第几页
        request.getSession().setAttribute("currentPage2",pageIndex2);

        // 地址重定向
        response.sendRedirect("admin/remarkQuery.jsp");

    }

    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        userInfo = (UserInfo) request.getSession().getAttribute("user");
        List<Remark> remarks = remarkDao.queryAll(userInfo.getUserId());
        List<BookInfo> rBooks = new ArrayList<>();

        for (Remark r : remarks) {
            rBooks.add(bookDao.queryById(r.getBookId()));
        }
        request.getSession().setAttribute("remarks", remarks);
        request.getSession().setAttribute("rBooks", rBooks);

        // 地址重定向
        response.sendRedirect("/user/center/user_12.jsp?page=1");
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        remarkDao.delete(id);
        request.getRequestDispatcher("/user/center/user_12.jsp?msg=1&page=1").forward(request, response);
    }
}
